/*
 * @Author: szx
 * @Date: 2021-10-26 11:47:08
 * @LastEditTime: 2021-10-26 11:56:34
 * @Description:
 * @FilePath: \leetcode\35\35.js
 */
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
// var searchInsert = function (nums, target) {
//     const n = nums.length;
//     let left = 0,
//         right = n - 1,
//         ans = n;
//     while (left <= right) {
//         let mid = ((right - left) >> 1) + left;
//         if (target <= nums[mid]) {
//             ans = mid;
//             right = mid - 1;
//         } else {
//             left = mid + 1;
//         }
//     }
//     return ans;
// };

var searchInsert = function (nums, target) {
    const n = nums.length;
    let left = 0,
        right = n;
    while (left < right) {
        let mid = ((right - left) >> 1) + left;
        if (target <= nums[mid]) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
};

function main() {
    const arr = [1, 3, 5, 6];
    // console.log('hello');
    console.log(searchInsert(arr, 5));
}
main();
